import xlrd

global SheetNumber
global caseDict
global dict

class excle_option:
    def __init__(self,filename,round):
        '''
        :param filename: test case file
        :param sheetNum: excel tab num
        :param round: testing round
        '''
        self.fname = filename
        self.round = round
        # self.shtNum = sheetNum
        # self.totalSheet = 0
        # 总用例数统计
        self.total_num = 0
        # 失败总数
        self.fail_count = 0
        # 阻塞总数
        self.block_count = 0
        # 通过总数
        self.pass_count = 0
        # 未测试总数
        self.notTest_count = 0
        # 废弃用例
        self.notAcpt_count = 0

    def getSheetNum(self):
        global SheetNumber
        workbook = xlrd.open_workbook(self.fname)
        all_SheetList = workbook.sheet_names()
        SheetNumber = len(all_SheetList)
        print('所有sheet名字：',all_SheetList)
        print('sheet总数',SheetNumber)
        # print(type(SheetNumber))
        return SheetNumber

    def split_sheetName(self,args):
        for each_sheetName in args:
            try:
                (model,owner) = each_sheetName.split('_',1)

            except ValueError:
                pass
        return model,owner


    # single sheet data
    def getSheetList(self,round,sheet_index,collect):
        '''
        :param round: case round
        :param sheet_index: which sheet
        :param collect: locate statistics positation
        :return:statistics result
        '''
        global caseDict
        caseDict = {}

        workbook = xlrd.open_workbook(self.fname)
        sheet = workbook.sheet_by_index(sheet_index)
        sheetName =sheet.name

        caseDict['Round'] = round

        # 处理sheet名字
        try:
            (model, owner) = sheetName.split('_', 1)
        except ValueError:
            pass
        caseDict['MODEL'] = model
        caseDict['OWNER'] = owner


        # sheetRows = sheet.nrows
        # sheetCols = sheet.ncols
        # print('sheet名字',sheetName)
        # print('sheet行数',sheetRows)
        # print('sheet列数',sheetCols)

        # 实例化待女子表对象
        Sheet = workbook.sheet_by_name(sheetName)

        for i in range(Sheet.nrows):
            rows = Sheet.row_values(i)
            # print(rows[8])
            if rows[collect] == 'FAIL':
                self.fail_count += 1
                self.total_num += 1
            elif rows[collect] == 'BLOCK':
                self.block_count += 1
                self.total_num += 1
            elif rows[collect] == 'PASS':
                self.pass_count += 1
                self.total_num += 1
            elif rows[collect] == 'NOT TEST':
                self.notTest_count += 1
                self.total_num += 1
            elif rows[collect] == 'NOT ACCESS':
                self.notAcpt_count += 1
                # self.total_num += 1
                # print(rows)
                # fp.write_file(rows)
        caseDict['Total'] = self.total_num
        caseDict['Fail'] = self.fail_count
        caseDict['Block'] = self.block_count
        caseDict['Pass'] = self.pass_count
        caseDict['NOT_TEST'] = self.notTest_count
        caseDict['NOT_ACCESS'] = self.notAcpt_count
        # print('caseDict', caseDict)
        return caseDict


        # print('Total %s Fail %s Block %s Pass %s NOT_TEST %s NOT_ACCESS %s' % (
        #     self.total_num, self.fail_count, self.block_count, self.pass_count, self.notTest_count, self.notAcpt_count))

    def collect_Data(self):
        # case file's sheet number
        global SheetNumber
        # save every round case result
        global roundList
        roundList = []

        result = excle_option(self.fname,self.round)
        result.getSheetNum()


        # result = excle_option(self.fname)

        # for i in range(SheetNumber):
        #     col = 8
        #     for r in range(self.round):
        #         # print(23 * '*第一' + str(self.round)+ 23 * '轮测试*')
        #         print(r)
        #         test_Round = result.getSheetList(r+1, i, col+(r-1)*5)
        #         # result.__init__(self.fname,self.round)
        #         roundList.append(test_Round)
        #         result.__init__(self.fname,self.round)
        #         # print('roundList', roundList)
        #
        #         # # print(23 * '*' + '第二轮测试' + 23 * '*')
        #         # result.__init__(self.fname)
        #         # second_round = result.getSheetList(2, i, col + 5)
        #         # roundList.append(second_round)
        #         # # print('roundList',roundList)
        #         #
        #         # # print(23 * '*' + '第三轮测试' + 23 * '*')
        #         # result.__init__(self.fname)
        #         # third_round = result.getSheetList(3, i, col + 10)
        #         # roundList.append(third_round)

        for i in range(SheetNumber):
            col = 8
            # print(23 * '*' + '第一轮测试' + 23 * '*')
            first_Round = result.getSheetList(1, i, 8)
            roundList.append(first_Round)
            # print('roundList', roundList)

            # print(23 * '*' + '第二轮测试' + 23 * '*')
            result.__init__(self.fname,self.round)
            second_round = result.getSheetList(2, i, col + 5)
            roundList.append(second_round)
            # print('roundList',roundList)

            # print(23 * '*' + '第三轮测试' + 23 * '*')
            result.__init__(self.fname,self.round)
            third_round = result.getSheetList(3, i, col + 10)
            roundList.append(third_round)
        print('roundList', roundList)
        return roundList

    def get_Model_info(self,owner=None,round=None,model=None):
        if owner != None:
            pass



class  file_option:
    def __init__(self,filename):
        self.fname = filename

    def write_file(self,args):
        with open(self.fname, 'w') as f:
            for each in args:
                f.write(each)


    def read_file(self):
        with open(self.fname, 'r') as f:
            list1 = f.readlines()


if __name__ == '__main__':
    rfile = 'E:/日常资料/工作需求文档/XU/XU1.0系统测试用例-vmware.xls'
    # saveFile = 'roundList.txt'
    # file_option = file_option(saveFile)

    data = excle_option(rfile,3)
    data.collect_Data()


    # sn = 3
    # col = 8
    # print(23 * '*' + '第一轮测试' + 23 * '*')
    # # result.getSheetList(sn, 8)
    # first_Round = result.getSheetList(1,sn, 8)
    # roundList.append(first_Round)
    # print('roundList',roundList)

    # print(23 * '*' + '第二轮测试' + 23 * '*')
    # result.__init__(rfile)
    # result.getSheetList(sn, col + 5)
    # print(23 * '*' + '第三轮测试' + 23 * '*')
    # result.__init__(rfile)
    # result.getSheetList(sn, col + 10)






